package com.harmontronics.hanma.paas.paas.domain.releasesetup.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.common.CrossDomainFunction;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.consts.EnvEnum;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.entity.HmReleaseRecord;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.entity.valueobject.HmReleaseRecordFields;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.entity.valueobject.HmReleaseSetupFields;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.entity.valueobject.ReleaseStatusEnum;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.repository.persistence.HmReleaseRecordRepository;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.repository.po.HmReleaseRecordPO;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.repository.po.HmReleaseSetupPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service("hmReleaseRecordService")
public class HmReleaseRecordService {

    @Autowired
    private HmReleaseRecordRepository hmReleaseRecordRepository;

    @Autowired
    private PackService packService;


    public HmReleaseRecord findById(Long id) {
        HmReleaseRecordPO po = hmReleaseRecordRepository.getById(id);
        return BeanUtil.toBean(po, HmReleaseRecord.class);
    }

    public void save(HmReleaseRecord hmReleaseRecord, String appName, String appIcon,List<String> modelSqls,CrossDomainFunction function) {
        HmReleaseRecordPO po = BeanUtil.toBean(hmReleaseRecord, HmReleaseRecordPO.class);
        po.setStatus(ReleaseStatusEnum.ONGOING);

        List<HmReleaseRecordPO> pos = hmReleaseRecordRepository.findByAppUniqueIdAndSetupType(hmReleaseRecord.getAppUniqueId(),hmReleaseRecord.getSetupType());
        if(pos.isEmpty()){
            po.setReleaseVersion("1");
        }else{
            int maxVersion = pos.stream().map(HmReleaseRecordPO::getReleaseVersion).mapToInt(Integer::parseInt).max().orElse(1);
            po.setReleaseVersion(String.valueOf(maxVersion+1));
        }
        hmReleaseRecordRepository.save(po);
        File appDir = packService.makeAppDir(po.getAppUniqueId());
        Object resultObj = function.cross(new File(appDir,"process"));
        if(!(Boolean)resultObj){
            po.setStatus(ReleaseStatusEnum.FAIL);
            hmReleaseRecordRepository.updateById(po);
            return;
        }
        packService.doPack(po,appDir,appName,appIcon,modelSqls);
    }


    public List<HmReleaseRecord> getList() {
        QueryWrapper<HmReleaseRecordPO> queryWrapper = new QueryWrapper<HmReleaseRecordPO>().orderByDesc(HmReleaseSetupFields.CREATE_DATE);
        List<HmReleaseRecordPO> pos = hmReleaseRecordRepository.list(queryWrapper);
        return Convert.toList(HmReleaseRecord.class, pos);
    }

    public PageBase<HmReleaseRecord> getList(EnvEnum setupType,int pageNo, int pageSize, String appUnique) {
        Page<HmReleaseRecordPO> page = new Page<>(pageNo, pageSize);
        QueryWrapper<HmReleaseRecordPO> queryWrapper = new QueryWrapper<>();
        //去除HM前缀
        String[] uniqueArr = appUnique.split("_");
        queryWrapper.eq(HmReleaseRecordFields.APP_UNIQUE_ID, uniqueArr[1]).
                orderByDesc(HmReleaseSetupFields.CREATE_DATE);
        if(setupType!=null){
            queryWrapper.eq(HmReleaseRecordFields.SETUP_TYPE,setupType);
        }
        IPage<HmReleaseRecordPO> resultPage =
                hmReleaseRecordRepository.selectPage(queryWrapper, page);


        PageBase<HmReleaseRecord> finalPage =  PageBase.from(resultPage, HmReleaseRecord.class);
        return finalPage;
    }

}